package com.sjc.counter.v3;

import com.sjc.counter.entity.RequestInfo;
import com.sjc.counter.entity.RequestStat;
import com.sjc.counter.v1.MetricsStorage;
import com.sjc.counter.v2.Aggregator;
import com.sjc.counter.v2.StatViewer;

import java.util.List;
import java.util.Map;

/**
 * @Classname ScheduledReporter
 * @Description
 * @Date 2020/5/1 18:17
 * @Created by SangJiacun
 */
public abstract class ScheduledReporter {
    protected MetricsStorage metricsStorage;
    protected Aggregator aggregator;
    protected StatViewer viewer;

    public ScheduledReporter(MetricsStorage metricsStorage, Aggregator aggregator, StatViewer viewer) {
        this.metricsStorage = metricsStorage;
        this.aggregator = aggregator;
        this.viewer = viewer;
    }

    protected  void doStatAndReport(long startTimeInMillis, long endTimeInMillis){
        long durationInMillis = endTimeInMillis - startTimeInMillis;
        //1. 从存储中得到指定时间的数据
        Map<String, List<RequestInfo>> requestInfos = metricsStorage.getRequestInfos(startTimeInMillis, endTimeInMillis);
        //2. 对返回的数据进行 分析
        Map<String, RequestStat> requestStats = aggregator.aggregate(requestInfos, durationInMillis);
        // 打印
        viewer.output(requestStats,startTimeInMillis,endTimeInMillis);
    }
}
